﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MediaPortal.GUI.Library;

namespace MyNetflix2
{
    public class Queue
    {
        public enum HistoryType { History, Shipped, Returned, Watched }

        #region Locals

        private User _user;
        List<QueueItem> _queueItems = new List<QueueItem>();
        List<ODataItem> _odataItems = new List<ODataItem>();
        private int _startIndex = 0;
        private int _endIndex = 100;
        private string _ETag;

        #endregion

        #region Public

        public int StartIndex
        {
            get { return _startIndex; }
            set { _startIndex = value; }
        }
        public int EndIndex
        {
            get { return _endIndex; }
            set { _endIndex = value; }
        }
        public User NetFlixUser
        {
            get { return _user; }
        }
        public List<QueueItem> QueueItems
        {
            get { return _queueItems; }
            set { _queueItems = value; }
        }
        public List<ODataItem> ODataItems
        {
            get { return _odataItems; }
            set { _odataItems = value; }
        }
        public string ETag
        {
            get { return _ETag; }
            set { _ETag = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="user"></param>
        public Queue(User user)
        {
            _user = user;
        }

        #endregion

        #region Virtual Methods

        public virtual void Refresh()
        { }

        public virtual List<QueueItem> Refresh(int iSelectedItem)
        { return null; }

        public virtual List<QueueItem> Refresh(string titleId, string theTVDbId)
        { return null; }

        public virtual List<QueueItem> Refresh(string titleId)
        { return null; }

        public virtual void Refresh(HistoryType historyType)
        { }

        public virtual void Add(string id, RestCalls.DiscFormat format, int position)
        { }

        public virtual List<ODataItem> GetTop(int iGenre)
        { return null; }

        #endregion

        #region Sort Methods

        /// <summary>
        /// Load the panel ...
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        /// <param name="sortBy">String to sort by Title or Year</param>
        public List<QueueItem> Sort(string sortBy)
        {
            switch (sortBy)
            {
                case "Title":
                    return SortByTitleQI();
                case "Year":
                    return SortByYear();
                default:
                    return _queueItems;
            }
        }


        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        public List<ODataItem> SortByDate(Formats.DiscFormat discFormat, SortOrder sortOrder)
        {
            for (int pass = 1; pass < _odataItems.Count; pass++)
            {
                for (int i = 0; i < _odataItems.Count - 1; i++)
                {
                    ODataItem list = (ODataItem)_odataItems[i];
                    ODataItem list2 = (ODataItem)_odataItems[i + 1];
                    DateTime date1 = new DateTime();
                    DateTime date2 = new DateTime();

                    foreach (Formats format in list.TitleFormats)
                    {
                        if (format.Format.ToString().ToLower() == discFormat.ToString().ToLower())
                        {
                            date1 = format.AvailableFrom;
                            break;
                        }
                    }
                    foreach (Formats format in list2.TitleFormats)
                    {
                        if (format.Format.ToString().ToLower() == discFormat.ToString().ToLower())
                        {
                            date2 = format.AvailableFrom;
                            break;
                        }
                    }

                    if (sortOrder == SortOrder.Ascending)
                    {
                        if (date1 > date2)
                            SwapOD(i);
                    }
                    else if (sortOrder == SortOrder.Descending)
                    {
                        if (date1 < date2)
                            SwapOD(i);
                    }
                }
            }
            return _odataItems;
        }

        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        public List<QueueItem> SortByYear()
        {
            for (int pass = 1; pass < _queueItems.Count; pass++)
            {
                for (int i = 0; i < _queueItems.Count - 1; i++)
                {
                    QueueItem list = (QueueItem)_queueItems[i];
                    QueueItem list2 = (QueueItem)_queueItems[i + 1];

                    QueueItem info = (QueueItem)list;
                    QueueItem info2 = (QueueItem)list2;

                    if (info.ReleaseYear < info2.ReleaseYear)
                        SwapQI(i);
                }
            }
            return _queueItems;
        }


        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        public List<QueueItem> SortByTitleQI()
        {
            for (int pass = 1; pass < _queueItems.Count; pass++)
            {
                for (int i = 0; i < _queueItems.Count - 1; i++)
                {
                    QueueItem list = (QueueItem)_queueItems[i];
                    QueueItem list2 = (QueueItem)_queueItems[i + 1];

                    QueueItem info = (QueueItem)list;
                    QueueItem info2 = (QueueItem)list2;
                    string title = info.Title.Replace("The ", "");
                    string title2 = info2.Title.Replace("The ", "");

                    if (String.Compare(title, title2) > 0)
                        SwapQI(i);
                }
            }
            return _queueItems;
        }

        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        public List<ODataItem> SortByTitleOD(List<ODataItem> movieInfo)
        {
            for (int pass = 1; pass < movieInfo.Count; pass++)
            {
                for (int i = 0; i < movieInfo.Count - 1; i++)
                {
                    ODataItem list = (ODataItem)movieInfo[i];
                    ODataItem list2 = (ODataItem)movieInfo[i + 1];

                    ODataItem info = (ODataItem)list;
                    ODataItem info2 = (ODataItem)list2;
                    string title = info.Title.Replace("The ", "");
                    string title2 = info2.Title.Replace("The ", "");

                    if (String.Compare(title, title2) > 0)
                        SwapOD(i);
                }
            }
            return movieInfo;
        }

        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        /// <param name="first"></param>
        public void SwapQI(int first)
        {
            QueueItem hold;

            hold = _queueItems[first];
            _queueItems[first] = _queueItems[first + 1];
            _queueItems[first + 1] = hold;
        }

        /// <summary>
        // sort the items of an array using bubble sort
        /// </summary>
        /// <param name="movieInfo">ArrayList of GuiListItems</param>
        /// <param name="first"></param>
        public void SwapOD(int first)
        {
            ODataItem hold;

            hold = _odataItems[first];
            _odataItems[first] = _odataItems[first + 1];
            _odataItems[first + 1] = hold;
        }

        #endregion
    }
}
